package com.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.DTO.examInfoDTO;
import com.exam.DTO.testInfo;
import com.exam.entity.ApiResult;
import com.exam.entity.ExamManage;
import com.exam.serviceimpl.ExamManageServiceImpl;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;

@RestController
public class ExamManageController {

    @Autowired
    private ExamManageServiceImpl examManageService;

    @GetMapping("/exams")
    public ApiResult findAll(){
        System.out.println("不分页查询所有试卷");
        ApiResult apiResult;
        apiResult = ApiResultHandler.buildApiResult(200, "请求成功！", examManageService.findAll());
        return apiResult;
    }

    @GetMapping("/exams/{page}/{size}")
    public ApiResult findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        System.out.println("分页查询所有试卷");
        ApiResult apiResult;
        Page<ExamManage> examManage = new Page<>(page,size);
        IPage<ExamManage> all = examManageService.findAll(examManage);
        apiResult = ApiResultHandler.buildApiResult(200, "请求成功！", all);
        return apiResult;
    }

    @GetMapping("/exam/{examCode}")
    public ApiResult findById(@PathVariable("examCode") Integer examCode){
        System.out.println("根据ID查找");
        ExamManage res = examManageService.findById(examCode);
        if(res == null) {
            return ApiResultHandler.buildApiResult(10000,"考试编号不存在",null);
        }
        return ApiResultHandler.buildApiResult(200,"请求成功！",res);
    }

    @DeleteMapping("/exam/{examCode}")
    public ApiResult deleteById(@PathVariable("examCode") Integer examCode){
        int res = examManageService.delete(examCode);
        return ApiResultHandler.buildApiResult(200,"删除成功",res);
    }

    @PutMapping("/exam")
    public ApiResult update(@RequestBody ExamManage exammanage){
        int res = examManageService.update(exammanage);
//        if (res == 0) {
//            return ApiResultHandler.buildApiResult(20000,"请求参数错误");
//        }
        System.out.print("更新操作执行---");
        return ApiResultHandler.buildApiResult(200,"更新成功",res);
    }

//    @PostMapping("/exam")
//    public ApiResult add(@RequestBody ExamManage exammanage){
//        int res = examManageService.add(exammanage);
//        if (res ==1) {
//            return ApiResultHandler.buildApiResult(200, "添加成功", res);
//        } else {
//            return  ApiResultHandler.buildApiResult(400,"添加失败",res);
//        }
//    }
    //根据题目数量计算总分
    @PostMapping("/exam")
    public ApiResult add1(@RequestBody examInfoDTO exammanage){
        String type=exammanage.getInfo().getType();
        int num11 = exammanage.getInfo().getNum1();
        String num1=String.valueOf(num11);
        int num12 = exammanage.getInfo().getNum2();
        String num2=String.valueOf(num12);
        int num13 = exammanage.getInfo().getNum3();
        String num3=String.valueOf(num13);
        String theme = exammanage.getInfo().getTheme();
        String simple =String.valueOf( exammanage.getInfo().getSimple());
        String normal = String.valueOf(exammanage.getInfo().getNormal());
        String diff = String.valueOf(exammanage.getInfo().getDiff());

        int sum = num11*2+num12*5+num13*10;

        ExamManage form =exammanage.getForm();
        String description = form.getDescription();
//        试卷名称
        String sourse = form.getSource();
        String examDate = form.getExamDate();
//        以分钟为单位
        String totalTime = String.valueOf(form.getTotalTime());
        String totalScore = String.valueOf(sum);
        String tips = form.getTips();
        System.out.println("开始");
        int res=0;

        try {
            // Python 脚本路径
            String pythonScriptPath = "D:/aPythonProject/zujuanSystem/paper/KgAssign.py";
            String pythonInterpreter = "D:/aAnaconda/envs/cuda/python.exe";
            // 调用 Python 脚本并传递参数
            ProcessBuilder processBuilder = new ProcessBuilder(pythonInterpreter,pythonScriptPath,type,num1,num2,num3,theme,simple,normal,diff,description,sourse,examDate,totalTime,totalScore,tips);
            processBuilder.redirectErrorStream(true);

            Process process = processBuilder.start();

            // 读取 Python 脚本的输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            res=1;
            // 等待 Python 脚本执行完成
            int exitCode = process.waitFor();
            System.out.println("Python script exited with code: " + exitCode);

        } catch (Exception e) {
            
        }
//            e.printStackTrace();
        
        if (res ==1) {
            return ApiResultHandler.buildApiResult(200, "添加成功", res);
        } else {
            return  ApiResultHandler.buildApiResult(400,"添加失败",res);
        }
    }

    @GetMapping("/examManagePaperId")
    public ApiResult findOnlyPaperId() {
        ExamManage res = examManageService.findOnlyPaperId();
        if (res != null) {
            return ApiResultHandler.buildApiResult(200,"请求成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"请求失败",res);
    }
    //设置试卷实际内容
//    @PostMapping("/exam/set")
//    public void set(@RequestBody testInfo testinfo){
//        System.out.println(testinfo);
//    }

}
